package com.hikeen.launcherx.functions.wifi;


import static com.hikeen.launcherx.utils.Constant.RSSI;
import static com.hikeen.launcherx.utils.Constant.WIFISTATE;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Message;
import android.util.Log;

import com.hikeen.launcherx.MyApplication;
import com.hikeen.launcherx.ui.MainActivity3;


public class WIFIUtils {

    private static WIFIUtils INSTANCE;
    private WifiBroadCastReceiver wifiBroadCastReceiver;
    public static WifiManager mWifiManager;
    private static String TAG = "WIFIUtils";
    private Context mContext;


    public static synchronized WIFIUtils getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new WIFIUtils();
        }
        return INSTANCE;
    }

    //注册广播接收器，用于监听WIFI状态变化
    public void registerWIFIStateReceiver(Context context) {
        mContext = context;
        wifiBroadCastReceiver = new WifiBroadCastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(wifiBroadCastReceiver, filter);
        wifiBroadCastReceiver.setOnWiFiStateListener(new WifiBroadCastReceiver.OnWIFIStateListener() {

            @Override
            public void onStateDisable() {
                //TODO:WIFI已关闭
                Log.d(TAG, "onStateDisable: ");
                Message message = Message.obtain();
                message.what = WIFISTATE;
                message.obj = false;
                ((MainActivity3) mContext).handler.sendMessage(message);
                WIFIState.getInstance().setSTATE(false);
            }

            @Override
            public void onStateEnable() {
                //TODO:WIFI已打开
                Log.d(TAG, "onStateEnable: ");
                Message message = Message.obtain();
                message.what = WIFISTATE;
                message.obj = true;
                ((MainActivity3) mContext).handler.sendMessage(message);
                WIFIState.getInstance().setSTATE(true);

            }

            @Override
            public void wifiAvailAble() {
                //TODO:WIFI是否可用
                Log.d(TAG, "wifiAvailAble: ");
                Message message = Message.obtain();
                message.what = 0x2222;//打开

            }

            @Override
            public void wifiChangedAble() {
                //TODO:WIFI状态发生变化
                Log.d(TAG, "wifiChangedAble: ");
            }

            @Override
            public void networkChangedAble(boolean f) {
                //TODO:网络状态发生变化
                WIFIState.getInstance().setHaveNetwork(f);
                if (f) {
                    Log.d(TAG, "networkChangedAble: ");
                    Message message = Message.obtain();
                    message.what = RSSI;//打开
                    message.arg1 = -1;
                    ((MainActivity3) mContext).handler.sendMessage(message);
                }
            }
        });
    }

    public void unregisterWiFiStateReceiver(Context context) {
        context.unregisterReceiver(wifiBroadCastReceiver);
    }

    //WIFI是否可用
    public static boolean isWiFiActive() {
        ConnectivityManager connectivity = (ConnectivityManager) MyApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] infos = connectivity.getAllNetworkInfo();
            if (infos != null) {
                for (NetworkInfo ni : infos) {
                    if (ni.getTypeName().equals("WIFI") && ni.isConnected()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 获取SSID
     *
     * @param
     * @return WIFI 的SSID
     */
    public static String getWIFISSID(Context mContext) {
        String ssid = "unknown id";

        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {//针对Android9.0 10.0 11.0的
            ConnectivityManager connManager = (ConnectivityManager) mContext.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert connManager != null;
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo.isConnected()) {
                if (networkInfo.getExtraInfo() != null) {
                    return networkInfo.getExtraInfo().replace("\"", "");
                }
            }
        } else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O || Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {

            mWifiManager = (WifiManager) MyApplication.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);

            assert mWifiManager != null;
            WifiInfo info = mWifiManager.getConnectionInfo();

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return info.getSSID();
            } else {
                return info.getSSID().replace("\"", "");
            }
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {

            ConnectivityManager connManager = (ConnectivityManager) MyApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert connManager != null;
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo.isConnected()) {
                if (networkInfo.getExtraInfo() != null) {
                    return networkInfo.getExtraInfo().replace("\"", "");
                }
            }
        }
        return ssid;
    }


    //   《获取wifi名称   已废弃》
    private String getConnectWifiSsid() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            ConnectivityManager cm = (ConnectivityManager) MyApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            assert cm != null;
            NetworkInfo info = cm.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                return info.getExtraInfo();
            }
        } else {
            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
            Log.d("wifiInfo", wifiInfo.toString());
            Log.d("SSID", wifiInfo.getSSID());
            return wifiInfo.getSSID();
        }
        return "未知网络";
    }
}
